import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
    private static final String[] OP = {"+","-","*","/"};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入您的身份证号:");
            String str = scanner.nextLine();//输入ID
            //验证ID，通过则进入继续运行，否则退出重新输入ID
            if (id(str)) {
                while (true) {
                    menu();//打印菜单
                    int i = scanner.nextInt();//输入选项
                    switch (i) {
                        case 1:
                            System.out.println("请稍等，正在获取问题......");
                            List<List<Integer>> person = test();//获取问题
                            System.out.println("问题以抽取完毕请开始答题：");
                            long time = 0;//初始化时间
                            int score = 0;//初始化分数
                            int num = 0;
                            long stime = System.currentTimeMillis();//开始计时
                            for (int j = 0; j < person.size(); j++) {
                                String shizi = person.get(j).get(0) + OP[person.get(j).get(3)] + person.get(j).get(1) + "=";
                                System.out.print(shizi);//输出题目
                                num = scanner.nextInt();//输入自己的运算结果
                                System.out.println(person.get(j).get(2));//输出正确答案
                                if (num == person.get(j).get(2)) {
                                    //每答对一题加十分
                                    score += 10;
                                }
                            }
                            long etime = System.currentTimeMillis();//结束计时
                            time = (etime - stime) / 1000;//计算时间
                            //向record.txt写入记录
                            try (FileWriter writer = new FileWriter("record.txt", true)) {
                                writer.append(str+ " " + score + " " + time + "seconds\n");
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case 2:
                            //读取文件中内容并输出
                            try (BufferedReader reader = new BufferedReader(new FileReader("record.txt"))) {
                                String line;
                                while ((line = reader.readLine()) != null) {
                                    System.out.println(line);
                                }
                            } catch (FileNotFoundException e) {
                                throw new RuntimeException(e);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.println();
                            break;
                        case 3:
                            //退出
                            return;
                        default:
                            break;
                    }
                }
            }
        }
    }
    public static boolean id(String str){
        if (str.length() != 6){
            //判断ID长度是否正确
            return false;
        }
        int countA = 0;//记录大写字母数量
        int countn = 0;//记录数字数量
        for (int i = 0; i < 6; i++) {
            if ((str.charAt(i) >= 'A' )&& (str.charAt(i) <= 'Z')){
                countA++;
            }else if (str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                countn++;
            }
        }
        //判断ID是否合规
        if (countn == 4&&countA == 2){
            return true;
        }else {
            return false;
        }
    }
    public static void menu(){
        System.out.println("(1) Start a test\n" +
                "(2) Check scores\n" +
                "(3) Exit");
    }
    private static List<List<Integer>> test(){
        List<List<Integer>> list = new ArrayList<>();//创建一个线性表，存储问题数字和运算符，前两个为运算数，第三个为正确答案，最后一个为运算符
        int[] getOp = new int[10];//创建一个数组，用于存放10个运算符
        for (int i = 0; i < 10; i++) {
            //获取运算符
            getOp[i] = get(getOp,i);
            //获取计算结果
            list.add(jisuan(getOp[i]));//添加数值
            list.get(i).add(getOp[i]);//添加运算符
        }
        return list;
    }
    //获取运算符
    private static int get(int[] getOp,int i){
        if (i == 0){
            return myRandom(4);
        }
        //处理相邻操作为不同运算符
        if (i < 8){
            return randomOp(i,getOp[i-1]);
        }
        //判断是否存在未出现过的运算符，若存在则返回该运算符，否则返回随机运算符
        int[] op = new int[4];
        for (int j =  i; j >= 0; j--) {
            op[getOp[j]]++;
        }
        for (int j = 0; j < 4; j++) {
            if (op[j] == 0) {
                return j;
            }
        }
        return randomOp(i,getOp[i-1]);
    }
    //获取与上一步操作不同的运算符
    private static int randomOp(int i,int ch){
        int op = myRandom(4);
        while (op == ch){
            op = myRandom(4);
        }
        return op;
    }
    // 计算答案(值大于0小于100)
    private static List jisuan(int op) {
        List<Integer> list = new ArrayList<>();
        int sum = 0;
        int x = 0,y = 0;
        switch (op){
            case 0:
                x = myRandom(97)+1;
                y = myRandom(99-x)+1;
                sum = x + y;
                break;
            case 1:
                x = myRandom(97)+2;
                y = myRandom(x)+1;
                sum = x - y;
                break;
            case 2:
                x = myRandom(98)+1;
                if (x>49){
                    y = 1;
                }else {
                    y = myRandom(100 / x - 1) + 1;
                }
                sum = x * y;
                break;
            case 3:
                x = myRandom(98)+1;
                if (x-1 == 0){
                    y = 1;
                }else {
                    y = myRandom(x-1)+1;
                }
                sum = x / y;
                break;
            default:
                break;
        }
        list.add(x);
        list.add(y);
        list.add(sum);

        return list;
    }
    //获取随机数
    private static int myRandom(int i){
        Random random = new Random();
        return random.nextInt(i);
    }
}